# _*_ coding : utf-8 _*_
# @Time : 2022/11/13 14:27
# @Author : TANGZZ
# @File : timer
# @Project : python_game
import pygame


# 定时任务
class TimerTask:
    def __init__(self, duration, func=None):
        # 执行周期
        self.duration = duration
        # 执行任务
        self.func = func
        # 是否正在计时
        self.active = False
        # 开始的时间
        self.start_time = 0

    def activateTask(self):
        self.start_time = pygame.time.get_ticks()
        self.active = True

    def deactivateTask(self):
        self.start_time = 0
        self.active = False

    def update(self):
        current_time = pygame.time.get_ticks()
        if current_time - self.start_time >= self.duration:
            if self.active:
                self.deactivateTask()
                if self.func:
                    self.func()
